package com.college.controller.user;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.college.common.constant.ErrorMsg;
import com.college.common.pojo.ao.Admin;
import com.college.common.pojo.ao.IdleItem;
import com.college.common.pojo.ao.User;
import com.college.common.pojo.vo.ResultVo;
import com.college.products.service.IdleItemService;
import com.college.products.service.OrderService;
import com.college.userInfo.service.AdminService;
import com.college.userInfo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("admin")
@Api(tags = "管理员")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private IdleItemService idleItemService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "管理员登录")
    @GetMapping("login")
    public ResultVo login(@RequestParam("accountNumber") @NotEmpty(message = "请输入账号") String accountNumber,
                          @RequestParam("adminPassword") @NotEmpty(message = "请输入密码") String adminPassword,
                          HttpSession session) {
        Admin admin = adminService.login(accountNumber, adminPassword);
        if (ObjectUtils.isEmpty(admin)) {
            return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
        }
        session.setAttribute("admin", admin);
        return ResultVo.success(admin);
    }

    @ApiOperation(value = "管理员登出")
    @GetMapping("loginOut")
    public ResultVo loginOut(HttpSession session) {
        session.removeAttribute("admin");
        return ResultVo.success();
    }

    @ApiOperation(value = "查询管理员列表")
    @GetMapping("list")
    public ResultVo getAdminList(@RequestParam(value = "page", required = false) Integer page,
                                 @RequestParam(value = "nums", required = false) Integer nums) {
        int p = 1;
        int n = 8;
        if (ObjectUtils.isNotEmpty(page)) {
            p = page > 0 ? page : 1;
        }
        if (ObjectUtils.isNotEmpty(nums)) {
            n = nums > 0 ? nums : 8;
        }
        return ResultVo.success(adminService.getAdminList(p, n));
    }

    @ApiOperation(value = "新增管理员")
    @PostMapping("add")
    public ResultVo addAdmin(@RequestBody Admin admin) {
        if (adminService.addAdmin(admin)) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.PARAM_ERROR);
    }

    @ApiOperation(value = "获取闲置商品集合")
    @GetMapping("idleList")
    public ResultVo idleList(@RequestParam("status") @NotNull(message = "状态不能为空") Integer status,
                             @RequestParam(value = "page", required = false) Integer page,
                             @RequestParam(value = "nums", required = false) Integer nums) {
        int p = 1;
        int n = 8;
        if (ObjectUtils.isNotEmpty(page)) {
            p = page > 0 ? page : 1;
        }
        if (ObjectUtils.isNotEmpty(nums)) {
            n = nums > 0 ? nums : 8;
        }
        return ResultVo.success(idleItemService.adminGetIdleList(status, p, n));
    }

    @ApiOperation(value = "更新闲置商品状态")
    @GetMapping("updateIdleStatus")
    public ResultVo updateIdleStatus(@RequestParam("id") @NotNull(message = "闲置商品不能为空") Long id,
                                     @RequestParam("status") @NotEmpty(message = "状态不能为空") Integer status
    ) {
        IdleItem idleItem = new IdleItem();
        idleItem.setId(id);
        idleItem.setIdleStatus(status.byteValue());
        if (idleItemService.updateIdleItem(idleItem)) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "获取订单集合")
    @GetMapping("orderList")
    public ResultVo orderList(@RequestParam(value = "page", required = false) Integer page,
                              @RequestParam(value = "nums", required = false) Integer nums) {
        int p = 1;
        int n = 8;
        if (ObjectUtils.isNotEmpty(page)) {
            p = page > 0 ? page : 1;
        }
        if (ObjectUtils.isNotEmpty(nums)) {
            n = nums > 0 ? nums : 8;
        }
        return ResultVo.success(orderService.getAllOrder(p, n));
    }

    @ApiOperation(value = "删除订单")
    @GetMapping("deleteOrder")
    public ResultVo deleteOrder(@RequestParam("id") @NotNull(message = "订单不能为空") Long id) {
        if (orderService.deleteOrder(id)) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "获取用户集合")
    @GetMapping("userList")
    public ResultVo userList(@RequestParam(value = "page", required = false) Integer page,
                             @RequestParam(value = "nums", required = false) Integer nums,
                             @RequestParam("status") @NotNull(message = "状态不能为空") Integer status) {
        int p = 1;
        int n = 8;
        if (ObjectUtils.isNotEmpty(page)) {
            p = page > 0 ? page : 1;
        }
        if (ObjectUtils.isNotEmpty(nums)) {
            n = nums > 0 ? nums : 8;
        }
        return ResultVo.success(userService.getUserByStatus(status, p, n));
    }

    @ApiOperation(value = "更新用户状态")
    @GetMapping("updateUserStatus")
    public ResultVo updateUserStatus(@RequestParam("id") @NotNull(message = "用户不能为空") Long id,
                                     @RequestParam("status") @NotNull(message = "状态不能为空") Integer status) {
        User user = new User();
        user.setId(id);
        user.setUserStatus(status.byteValue());
        if (userService.updateUserInfo(user))
            return ResultVo.success();
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "按订单闲置物品名称查询")
    @GetMapping("queryIdle")
    public ResultVo queryIdle(@RequestParam(value = "findValue", required = false) String findValue,
                              @RequestParam(value = "page", required = false) Integer page,
                              @RequestParam(value = "nums", required = false) Integer nums,
                              @RequestParam("status") @NotNull(message = "状态不能为空") Integer status) {
        if (ObjectUtils.isEmpty(findValue)) {
            findValue = "";
        }
        int p = 1;
        int n = 8;
        if (ObjectUtils.isNotEmpty(page)) {
            p = page > 0 ? page : 1;
        }
        if (ObjectUtils.isNotEmpty(nums)) {
            n = nums > 0 ? nums : 8;
        }

        if (status == 1){
            return ResultVo.success(idleItemService.findIdleItem(findValue, p, n));
        }
        return ResultVo.success(idleItemService.findIdleItem1(findValue, status, p, n));

    }

    @ApiOperation(value = "按订单号查询订单")
    @GetMapping("queryOrder")
    public ResultVo queryOrder(@RequestParam(value = "page", required = false) Integer page,
                               @RequestParam(value = "nums", required = false) Integer nums,
                               @RequestParam(value = "searchValue", required = false) String searchValue) {
        int p = 1;
        int n = 8;
        if (ObjectUtils.isNotEmpty(page)) {
            p = page > 0 ? page : 1;
        }
        if (ObjectUtils.isNotEmpty(nums)) {
            n = nums > 0 ? nums : 8;
        }

        if (StringUtils.isBlank(searchValue)){
            return ResultVo.success(orderService.getAllOrder(p, n));
        }
        return ResultVo.success(orderService.findOrderByNumber(searchValue, p, n));
    }

    @ApiOperation(value = "根据用户账号来查找信息")
    @GetMapping("queryUser")
    public ResultVo queryUser(@RequestParam(value = "searchValue", required = false) String searchValue,
                              @RequestParam(value = "mode", required = false) Integer mode,
                              @RequestParam(value = "page", required = false) Integer page,
                              @RequestParam(value = "nums", required = false) Integer nums) {
        int p = 1;
        int n = 8;
        if (ObjectUtils.isNotEmpty(page)) {
            p = page > 0 ? page : 1;
        }
        if (ObjectUtils.isNotEmpty(nums)) {
            n = nums > 0 ? nums : 8;
        }
        if (mode == 1) {
            if (StringUtils.isBlank(searchValue)) {
                return ResultVo.success(userService.getUserByStatus(0, p, n));
            } else {
                return ResultVo.success(userService.getUserByNumber(searchValue, mode));
            }
        } else if (mode == 2) {
            if (StringUtils.isBlank(searchValue)) {
                return ResultVo.success(userService.getUserByStatus(1, p, n));
            } else {
                return ResultVo.success(userService.getUserByNumber(searchValue, mode));
            }
        } else
            return ResultVo.success(adminService.getAdminList(p, n));
    }

}
